package com.chen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.domain.SysMenu;
import com.chen.domain.SysPrivilege;
import com.chen.domain.SysRoleMenu;
import com.chen.domain.SysRolePrivilege;
import com.chen.mapper.SysMenuMapper;
import com.chen.model.RolePrivilegesParam;
import com.chen.service.SysMenuService;
import com.chen.service.SysPrivilegeService;
import com.chen.service.SysRoleMenuService;
import com.chen.service.SysRolePrivilegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Autowired
    private SysPrivilegeService sysPrivilegeService;

    @Autowired
    private SysRolePrivilegeService sysRolePrivilegeService;

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    @Resource
    private SysMenuMapper sysMenuMapper;

    @Autowired
    private SysMenuService sysMenuService;


    @Override
    public List<SysMenu> findUserMenu(Long id) {
        return sysMenuMapper.selectUserMenu(id);
    }


    @Override
    public List<SysMenu> findRolePrivileges(String roleId) {
        //查询所有的菜单
        List<SysMenu> sysMenus = list();

        //找出所有菜单中，最低级的菜单【没有任何一个菜单把它的id当作parentId】
        List<SysMenu> findMenus = new ArrayList<>();
        //开始循环 【长】
        for (SysMenu sysMenu : sysMenus) {
            //创建一个标志【判定是否为】最低级
            boolean flag = false;
            //继续循环查询 【宽】
            for (SysMenu sysMenu1 : sysMenus) {
                //如果 长 ==  宽  继续查询 同时修改标记为true
                if (sysMenu.getId().equals(sysMenu1.getParentId())) {
                    flag = true;
                    break;
                }
            }
            if (!flag) {
                findMenus.add(sysMenu);
            }
        }
        List<SysPrivilege> sysPrivileges = sysPrivilegeService.list();

        QueryWrapper<SysRolePrivilege> sysRolePrivilegeQueryWrapper = new QueryWrapper<>();
        QueryWrapper<SysRolePrivilege> rolePrivilege = sysRolePrivilegeQueryWrapper.eq("role_id", roleId);
        List<SysRolePrivilege> sysRolePrivileges = sysRolePrivilegeService.list(rolePrivilege);


        //判断所查询出来的菜单是否拥有权限
        for (SysMenu sysMenu : findMenus) {
            List<SysPrivilege> sysPrivilegesX = new ArrayList<>();
            for (SysPrivilege sysPrivilege : sysPrivileges) {
                if (sysMenu.getId().equals(sysPrivilege.getMenuId())) {
                    for (SysRolePrivilege sysRolePrivilege : sysRolePrivileges) {
                        if (sysRolePrivilege.getPrivilegeId().equals(sysPrivilege.getId())) {
                            sysPrivilege.setOwn(1);
                        }
                    }
                    sysPrivilegesX.add(sysPrivilege);
                }

            }
            sysMenu.setPrivileges(sysPrivilegesX);

        }

//
//        //剥离所有的二级菜单
//        List<SysMenu> topMenu = new ArrayList<>();
//        for(SysMenu sysMenu : sysMenus){
//            if(sysMenu.getParentId() == null){
//                topMenu.add(sysMenu);
//            }
//        }
//        //得到了二级菜单
//        List<SysMenu> twoMenu = new ArrayList<>();
//        for(SysMenu sysMenu : topMenu){
//            List<SysMenu> menuList = getSysMenusByParentId(sysMenu.getId());
//            twoMenu.addAll(menuList);
//        }
//        //递归所有的二级菜单下的子菜单
//        for(SysMenu sysMenu : twoMenu){
//            sysMenu.setChilds(getChilds(sysMenu.getId(),Long.parseLong(roleId),sysMenu));
//        }
//        //平凑数据返回
        return findMenus;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void grantPrivileges(RolePrivilegesParam rolePrivilegesParam) {

        //先把原有权限删除
        sysPrivilegeService.removeByRoleId(rolePrivilegesParam.getRoleId());

        //保存资源数据
        List<SysMenu> sysMenus = new ArrayList<>();

        //平凑需要保存的 角色 - 权限数据
        List<SysRolePrivilege> sysRolePrivileges = new ArrayList<>();
        String[] privilegeIds = rolePrivilegesParam.getPrivilegeIds();
        for (String privilegeId : privilegeIds) {
            SysMenu sysMenu = sysPrivilegeService.findMenuById(privilegeId);
            SysRolePrivilege sysRolePrivilege = new SysRolePrivilege();
            sysRolePrivilege.setPrivilegeId(Long.parseLong(privilegeId));
            sysRolePrivilege.setRoleId(Long.parseLong(rolePrivilegesParam.getRoleId()));
            sysRolePrivileges.add(sysRolePrivilege);
            sysMenus.add(sysMenu);

        }

        //查询当前权限所对应菜单中 父菜单
        List<Long> menuIds = findParentMenu(sysMenus);

        sysRoleMenuService.removeByMenuIds(menuIds);

        //构造数据
        List<SysRoleMenu> sysRoleMenus = new ArrayList<>();

        for (Long menuId : menuIds) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();

            sysRoleMenu.setMenuId(menuId);
            sysRoleMenu.setRoleId(Long.parseLong(rolePrivilegesParam.getRoleId()));

            sysRoleMenus.add(sysRoleMenu);
        }
        //保存数据
        sysRoleMenuService.saveBatch(sysRoleMenus);

        //再赋值新权限
        sysRolePrivilegeService.saveBatch(sysRolePrivileges);


    }

    @Override
    public SysMenu findParentById(Long parentId) {
        return sysMenuMapper.selectParentById(parentId);
    }

    @Override
    public List<SysMenu> findUserRoleMenu(String id) {
        return sysMenuMapper.selectUserMenu(Long.parseLong(id));
    }


    private List<Long> findParentMenu(List<SysMenu> sysMenus) {

        //对现有的最低级的菜单进行 parentId 的去重复
        Set<Long> parentIds = new HashSet<>();
        for (SysMenu sysMenu : sysMenus) {
            parentIds.add(sysMenu.getParentId());
        }

        //查询新的父id
        List<Long> result = new ArrayList<>();
        for (Long parentId : parentIds) {
            SysMenu sysMenu = sysMenuService.findParentById(parentId);
            result.add(sysMenu.getId());
        }

        result.addAll(parentIds);

        return result;


    }

//    private List<SysMenu> getSysMenusByParentId(Long id) {
//        QueryWrapper<SysMenu> objectQueryWrapper = new QueryWrapper<>();
//        objectQueryWrapper.eq("parent_id",id);
//        return sysMenuMapper.selectList(objectQueryWrapper);
//    }

//    private List<SysMenu> getChilds(Long id,Long roleId,SysMenu sysMenuOld) {
//
//        List<SysMenu> sysMenusByParentId = getSysMenusByParentId(id);
//        if(CollectionUtils.isEmpty(sysMenusByParentId)){
//            List<SysPrivilege> privilege = sysPrivilegeService.findMenuPrivilege(id, roleId);
//            sysMenuOld.setPrivileges(privilege);
//            return Collections.emptyList();
//        }
//
//
//        //同时查询角色id所对应的权限与该子菜单的权限是否一致【role - per  == menu - per】 role  --  menu
//
//        for(SysMenu sysMenu : sysMenusByParentId){
//            List<SysMenu> childs = getChilds(sysMenu.getId(),roleId,sysMenuOld);
//            sysMenu.setChilds(childs);
//            List<SysPrivilege> privilege = sysPrivilegeService.findMenuPrivilege(sysMenu.getId(), roleId);
//            sysMenu.setPrivileges(privilege);
//        }
//
//
//        return sysMenusByParentId;
//    }
}
